package LeetCode.leetcode654;

public class Review1 {
    public static void main(String[] args) {
        int[] x = {3,2,1,6,0,5};
        constructMaximumBinaryTree(x);
    }
    public static TreeNode constructMaximumBinaryTree(int[] nums) {
        return test(nums,0,nums.length - 1);
    }
    public static TreeNode test(int[] nums, int left ,int right){
        if (left > right) return null;
        int max = Integer.MIN_VALUE;
        int index = left;
        for (int i = left; i <= right; i++) {
            if (max < nums[i]) {
                max = nums[i];
                index = i;
            }
        }
        TreeNode node = new TreeNode(nums[index]);
        node.left = test(nums,left,index - 1);
        node.right = test(nums,index + 1,right);
        return node;
    }
}
